package cn.lhl01.common;

import io.jsonwebtoken.security.Keys;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;

import javax.crypto.SecretKey;
import java.nio.charset.StandardCharsets;

/**
 * 接口返回通用封装
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Results<T> {


	public static final String SECRET = "12345678901234567890123456789012345678901234567890";
	public static final SecretKey SECRET_KEY = Keys.hmacShaKeyFor(Results.SECRET.getBytes(StandardCharsets.UTF_8));

	public static final Long EXPIRE_TIME = 1000L * 60 * 60 * 24; //1天期
	/**
	 * 成功code
	 */
	private static final int SUCCESS_CODE = HttpStatus.OK.value();
	/**
	 * 成功 msg
	 */
	private static final String SUCCESS_MSG = "SUCCESS";
	/**
	 * 参数错误 code
	 */
	private static final int PARAM_ERROR_CODE = HttpStatus.BAD_REQUEST.value();

	//=======================================================================================================
	/**
	 * 拒绝访问 code
	 */
	private static final int STATE_ACCESS_DENIED = HttpStatus.FORBIDDEN.value();
	/**
	 * 拒绝访问 msg
	 */
	private static final String MESSAGE_ACCESS_DENIED = "没有权限,拒绝访问";
	/**
	 * Token无效或者已经过期 code
	 */
	private static final int STATE_EXPIRE = HttpStatus.UNAUTHORIZED.value();
	/**
	 * Token无效或者已经过期 msg
	 */
	private static final String MESSAGE_EXPIRE = "token无效或者已经过期";
	/**
	 * 系统异常 code
	 */
	private static final int STATE_SERVER_ERROR = HttpStatus.INTERNAL_SERVER_ERROR.value();

	//=======================================================================================================
	/**
	 * Token无效或者已经过期 msg
	 */
	private static final String MESSAGE_STATE_SERVER_ERROR = "系统异常,请联系管理员";
	private int code;

	//=======================================================================================================
	private String msg;
	private T data;

	public static Results success() {
		return new Results(SUCCESS_CODE, SUCCESS_MSG, null);
	}

	//=======================================================================================================

	public static Results success(String msg) {
		return new Results(SUCCESS_CODE, msg, null);
	}

	public static Results success(Object data) {
		return new Results(SUCCESS_CODE, SUCCESS_MSG, data);
	}

	public static Results paramError(String message) {
		return new Results(PARAM_ERROR_CODE, message, null);
	}

	//=======================================================================================================

	public static Results accessDenied() {
		return new Results(STATE_ACCESS_DENIED, MESSAGE_ACCESS_DENIED, null);
	}

	public static Results tokenExpired() {
		return new Results(STATE_EXPIRE, MESSAGE_EXPIRE, null);
	}

	public static Results serverError() {
		return new Results(STATE_SERVER_ERROR, MESSAGE_STATE_SERVER_ERROR, null);
	}

	public static Results serverError(String msg) {
		return new Results(STATE_SERVER_ERROR, msg, null);
	}

	//=======================================================================================================

	//业务型的异常
	public static Results apiError(ApiException apiException) {
		return new Results(apiException.getCode(), apiException.getMessage(), null);
	}

}

